import log from '@feidao/web/atom/nodejs/logger/log';
import query from '@feidao/web/atom/nodejs/db/query';
import count from '@feidao/web/atom/nodejs/db/count';
import sql_query from '@feidao/web/atom/nodejs/db/sql_query';
import complex_query from '@feidao/web/atom/nodejs/db/complex-query';
import format from '@feidao/web/atom/nodejs/dt/format';
import parse from '@feidao/web/atom/core/dt/parse';
import { systemid } from './config';

interface Message {
	execute_no: string;
	proj_name: string;
	project_type: string;
	member_classify: string;
	docstatus: string;
	page_no: number;
	page_size: number;
	sort: {
		num: number;
		filed: string;
	};
	afdate: string;
	bfdate: string;
}


export default async function atom(msg: Message, action_id: string, session_id: string, spaceid: string) {
	log('server start');
	log('server end');

	const { exec, prepare } = query(action_id, session_id, spaceid, systemid);

	prepare('atom_01', ['_id', 'a', 'b', 'c', 'd', 'e', 'f', 'g'], {});
	const data1 = await exec();

	prepare('atom_02', ['a'], {});
	const [data2] = await exec();

	const { prepare: c_prepare, exec: c_exec } = count(action_id, session_id, spaceid, systemid);
	c_prepare('atom_01', {});
	c_prepare('atom_02', {});
	const [count1, count2] = await c_exec();

	log('查询不同的数据库即不同的systemid下的表');
	const q_obj = query(action_id, session_id, spaceid, '12');
	q_obj.prepare('pub_user', ['_id', 'usercode'], {});
	// q_obj.prepare('pub_user', ['_id', 'usercode', 'user_code'], {});
	const data3 = await q_obj.exec();

	//判断传入的值是否为空
	let find_str = '';
	if (msg.execute_no) {
		find_str = find_str + `and execute_no like '%${msg.execute_no}%' `
	}
	if (msg.proj_name) {
		find_str = find_str + `and proj_name like '%${msg.proj_name}%' `
	}
	if (msg.project_type) {
		find_str = find_str + `and project_type = '${msg.project_type}' `
	}
	if (msg.member_classify) {
		find_str = find_str + `and member_classify = '${msg.member_classify}' `
	}
	if (msg.docstatus) {
		find_str = find_str + `and docstatus = '${msg.docstatus}' `
	}

	//假分页
	if (!msg.page_no) {
		msg.page_no = 1;
	}

	if (!msg.page_size) {
		msg.page_size = 20;
	}


	//排序
	//	enterdate
	let sort;

	if (msg.sort) {
		if (msg.sort.num == -1) {
			sort = `order by ${msg.sort.filed} desc`
		} else if (msg.sort.num == 1) {
			sort = `order by ${msg.sort.filed} asc`
		}
	} else {
		sort = `order by enterdate desc`
	}

	const sql = `
	select
		_id
		,execute_no
		,docstatus
		,relevance_no
		,proj_name
		,purchase_unit
		,unit_no
		,unit_name
		,acquisition_methods
		,industry_category
		,member_classify
		,checkform_type
		,compact_type
		,project_type
		,is_other_modify
		,task_start_time
		,finish_time
		,contact
		,contacts_tel
		,memo
		,entername
		,to_char(to_timestamp(enterdate/1000),'yyyy-MM-dd hh24:mi') as enterdate
		,update_briefly
		,delay_time
		,evaluation_standard
		,bid_evaluation
		,operation_after_expiration
	from
		purchase_execution_order a
	where
		enterdate <= ${parse(msg.afdate) - 8 * 60 * 60 * 1000}
		and enterdate >= ${parse(msg.bfdate) + 16 * 60 * 60 * 1000}
		and acquisition_methods = '询价采购' ${find_str}
	${sort}
	limit
		${msg.page_size}
	offset
		${(msg.page_no - 1) * msg.page_size} `;

	// 1.传递的参数和数据库字段类型不匹配
	// 2.查询条件为空要判断
	// 3.sql语句的使用

	const count3 = `
	select
		count(1)
	from
		purchase_execution_order
	where
		enterdate <= ${parse(msg.afdate)}
		and enterdate >= ${parse(msg.bfdate)}
		and acquisition_methods = '询价采购' ${find_str}
	`;


	const [ret, ret1] = await exec();

	const sql_q_obj = sql_query(action_id, session_id, spaceid, '12');
	sql_q_obj.prepare('select _id, usercode from pub_user');
	sql_q_obj.prepare(sql);
	sql_q_obj.prepare(count3);
	const data4 = await sql_q_obj.exec();

	const c_query = complex_query(action_id, spaceid, '12');
	const data5 = await c_query.add_field('pub_user', '_id', 'id').add_field('pub_user', 'usercode', 'user_code').exec();

	// 1.客户端与服务端atom不能通用。
	// 2.第三方依赖缺失。

	// format操作，两个参数，一个参数为时间戳(毫秒)[https://feidao-edu.gitee.io/atom-api/nodejs/dt/index.html]

	//年 year  月 month  日 day  小时 hour  分钟 minute  秒 second
	data2.forEach((item: any) => {
		item.month_date_str = format(item.month_date, 'yyyy-mm-dd');
		item.start_time_str = format(item.start_time, 'yyyy-mm-dd HH:MM');
		item.finish_time_str = format(item.finish_time, 'yyyy-mm-dd HH:MM');
		item.finish_time_str = format(item.finish_time, 'yyyy-mm-dd hh:MM');
	});

	// 	const time = 1530848800337;
	// console.log(format(time, 'yyyy-mm-dd'));  // 2018-07-06
	// console.log(format(time, 'yy-mm-dd')); // 18-07-06
	// console.log(format(time, 'yyyy-m-dd')); // 2018-7-06
	// console.log(format(time, 'yyyy-mm-d')); // 2018-07-6
	// console.log(format(time, 'yyyy-mm-dd HH:MM:ss')); // 2018-07-06 11:46:40
	// console.log(format(time, 'yyyy-mm-dd hh:MM:ss')); // 2018-07-06 11:46:40
	// console.log(format(time, 'yyyy-mm-dd hh:MM')); // 2018-07-06 11:46
	// console.log(format(time, 'yyyy-mm-dd hh')); // 2018-07-06 11
	// console.log(format(time, 'yyyy-mm-dd MM:ss')); // 2018-07-06 46:40
	// console.log(format(time, 'mm-dd MM:ss')); // 07-06 46:40

	return { data1: data1[0], data2, count1, count2, data3, data4, data5 };
}
